﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using TV_Agenda_V2.Objects;

namespace TV_Agenda_V2
{
    public partial class SearchResultsWindow : Form
    {
        public List<Show> Results { get; set; }
        public Show SelectedShow { get; set; }

        private Boolean even = false;

        public SearchResultsWindow()
        {
            InitializeComponent();
            DialogResult = DialogResult.Cancel;
        }

        public DialogResult ShowDialog(Form owner, List<Show> results)
        {
            Results = results;
            lblTitle.Text = String.Format(lblTitle.Text, results.Count);
            tlpShows.RowCount = results.Count + 1;
            tlpShows.RowStyles.Clear();
            int i = 0;
            foreach (Show s in results)
                AddShow(s, i++);
            
            ClientSize = new Size(300, (25 * results.Count) + (int)tableLayoutPanel1.RowStyles[0].Height);
            
            return ShowDialog(owner);
        }

        private void AddShow(Show show, int row)
        {
            tlpShows.RowStyles.Add(new RowStyle(SizeType.Absolute, 25));
            tlpShows.Controls.Add(CreateLabel(show), 0, row);
        }

        private Control CreateLabel(Show show)
        {
            Label label = new Label();
            label.Name = show.ID.ToString();
            label.Text = show.Name;
            label.TextAlign = ContentAlignment.MiddleLeft;
            label.Margin = new Padding(0);
            label.MouseEnter += new EventHandler(label_MouseEnter);
            label.MouseLeave += new EventHandler(label_MouseLeave);
            label.Click += new EventHandler(label_Click);
            label.Font = new Font(label.Font, FontStyle.Bold);
            //label.Image = new Bitmap("http://images.tvrage.net/flags/us.gif");
            label.Dock = DockStyle.Fill;
            if (even)
                label.BackColor = Color.Gray;
            else
                label.BackColor = Color.DarkGray;
            even = !even;
            return label;
        }

        public void label_Click(object sender, EventArgs e)
        {
            Label l = (Label)sender;
            Int32 ID = Int32.Parse(l.Name);
            foreach (Show s in Results)
            {
                if (s.ID == ID)
                {
                    SelectedShow = s;
                    DialogResult = DialogResult.OK;
                    this.Close();
                }
            }
        }

        void label_MouseLeave(object sender, EventArgs e)
        {
            Cursor = Cursors.Default;
        }

        void label_MouseEnter(object sender, EventArgs e)
        {
            Cursor = Cursors.Hand;
        }

        private void SearchResultsWindow_ResizeEnd(object sender, EventArgs e)
        {
            Console.WriteLine("Size: " + Size.ToString());
        }

        private void SearchResultsWindow_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                Label l = (Label)tlpShows.Controls[0];
                Int32 ID = Int32.Parse(l.Name);
                foreach (Show s in Results)
                {
                    if (s.ID == ID)
                    {
                        SelectedShow = s;
                        DialogResult = DialogResult.OK;
                        this.Close();
                    }
                }
            }
        }
    }
}
